import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.Scanner;

public class GraphicsController extends JPanel{
	
	//all initializing local variables
	public static int radius = 40;
	HexGrid world;
    Sprite test = new Circle ("A", radius, Color.RED);
    Sprite test2 = new Circle ("B", radius, Color.BLUE);
	GoalSprite test3 = new GoalCircle("C", radius, Color.ORANGE);
	GoalSprite test4 = new GoalCircle("D", radius, Color.WHITE);
	BufferedImage background;
	
	//constructor
	public GraphicsController(int row, int col, int depth){
	    test4.setTarget(test);
	    test3.setTarget(test2);
		world = new HexGrid(row, col, depth);
		int[] randomSpot = world.RandomValidLocation();
		world.addToGridAt(test, randomSpot[0], randomSpot[1], randomSpot[2]);
		randomSpot = world.RandomValidLocation();
		world.addToGridAt(test2, randomSpot[0], randomSpot[1], randomSpot[2]);
		randomSpot = world.RandomValidLocation();
		world.addToGridAt(test3, randomSpot[0], randomSpot[1], randomSpot[2]);
		randomSpot = world.RandomValidLocation();
		world.addToGridAt(test4, randomSpot[0], randomSpot[1], randomSpot[2]);
		background = createImage("kitteh2.jpg");
	}
	
	
	//where all the painting functions will be called
	public void paint(Graphics g){
		g.drawImage(background, 0, 0, this);//for drawing the background
		world.paint(g);
	}
    
    //Read image from file and return
    private BufferedImage createImage(String filename){
    	BufferedImage bufferedImage = new BufferedImage(500,375,BufferedImage.TYPE_INT_RGB);
    	File f = new File(filename);
    	try {
			bufferedImage = ImageIO.read(f);
		} catch (IOException e) {
			e.printStackTrace();
		}
    	return bufferedImage;
    }
	
	
	public static void main(String[] args){
		int rows = 10, cols = 15, depth = 5;
		/*System.out.println("Enter rows, columns, depth dimensions:");
		Scanner in = new Scanner(System.in);
		rows = in.nextInt();
		cols = in.nextInt();
		depth = in.nextInt();
		in.close();*/
		JFrame frame = new JFrame();
		GraphicsController view = new GraphicsController(rows,cols,depth);
    	frame.getContentPane().add(view);
    	frame.setBackground(Color.GRAY);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setSize(900, 750);
    	frame.setVisible(true);
    	
    	view.world.moveAll();
    	for(int i = 0; i < 1000; i++){
    		//view.world.moveAll();
    		view.world.setAll();
    		for(int j = 0; j <= 24; j++){
    			frame.repaint();
    			try {
					Thread.sleep(20);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    		}
    		view.world.moveAll();
    		//try {
		//		Thread.sleep(300);
		//	} catch (InterruptedException e) { e.printStackTrace(); }
    	}
	}	
}
